$(function () {

    var stockincome_datagrid = $("#stockincome_datagrid");
    var stockincome_dialog = $("#stockincome_dialog");
    var stockincome_dialog_datagrid = $("#stockincome_dialog_datagrid")
    var SelectProduct_datagrid = $("#SelectProduct_datagrid")
    var SelectProduct_dialog = $("#SelectProduct_dialog")
    var stockincome_dialog_form = $("#stockincome_dialog_form")


    var stockincomeData = null;

    stockincome_dialog.dialog({
        title:"修改商品采购单",
    })




    //选中商品的列表
    SelectProduct_datagrid.datagrid({
        url:"/product/list",
        fit: true,
        fitColumns: true,
        singleSelect: true,
        columns: [[
            {field: 'id', title: '产品名称', hidden:true, width: 100},
            {field: 'productName', title: '产品名称', width: 100},
            {field: 'sn', title: '产品编号', width: 100},
            {field: 'unit', title: '单位', width: 100},
            {field: 'cosePrice', title: '售价', width: 100},
            {field: 'number', title: '数量', width: 100},
            {field: 'income', title: '入库时间', width: 100},
        ]],
        onDblClickRow:function (index,row) {
            console.log(row);
            //1.获取行数据
            stockincome_dialog_datagrid.datagrid("appendRow",{
                id:row.id,
                sn:row.sn,
                productName:row.productName,
                productId:row.id,
                unit:row.unit,
                number:row.number,
                cosePrice:row.cosePrice,
                amountPrice:row.cosePrice*row.number

            });

            SelectProduct_dialog.dialog("close")

        }
    })

    stockincome_dialog_datagrid.datagrid({
        fit:true,
        fitColumns:true,
        singleSelect:true,
        columns:[[
            {field:"id",title:"编号",width:100,hidden:true},
            {field:"product.id",title:"编号",width:100,hidden:true},
            {field:"sn",title:"商品编码",width:100},
            {field:"productName",title:"商品名称",width:100},
            {field:"unit",title:"单位",width:100},
            {field:"number",title:"数量",width:100},
            {field:"cosePrice",title:"单价",width:100},
            {field:"amountPrice",title:"金额",width:100}
        ]],
        onLoadSuccess:function (data) {

            console.log("DATA=="+data);
        }
    })



    //列表
    stockincome_datagrid.datagrid({
        url:"/stockincome/list",
        fit:true,
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        border:false,
        columns:[[

                {field:"sn",title:"订单编码",width:100},
                {field:"supplier",title:"供应商",width:100,formatter:formatterSupplier},
                {field:"costPrice",title:"单价",width:100},
                {field:"number",title:"数量",width:100},
                {field:"amountPrice",title:"合计",width:100},
                {field:"receipts",title:"实收",width:100},
                {field:"state",title:"入库状态",width:100,formatter:formatterState},
                {field:"operator",title:"操作人员",width:100,formatter:formatterOperator},
                //{field:"Action",title:"操作",width:100,formatter:formatterOperatorStatus}
        ]],
        toolbar:"#stockincome_toolbar",
        onLoadSuccess:function (data) {
            console.log(data);
            stockincomeData = $.extend(true,{},data.bills);
            console.log(stockincomeData)
        },
        onClickCell: onClickCell
    })
    
    cmdObj = {

        //刷新界面
        reload:function () {
            stockincome_datagrid.datagrid("reload");
        },
        //界面消失
        cancel:function () {
            alert(1)
            stockincome_dialog.dialog("close")

        },

        //新增货品
        stockincomeAdd:function () {
            //1.请求表单
            stockincome_dialog_form.form("clear");
            stockincome_dialog_datagrid.datagrid('loadData', { total: 0, rows: [] });
            //2.弹出diaolog
            stockincome_dialog.dialog("open")


        },
        //导出打印
        stockincomeExport:function () {
            
        },
        
        //确认入库
        sureInProductStock:function () {
            //1.选中行
            var selectedRow = stockincome_datagrid.datagrid("getSelected");
            console.log(selectedRow);
            if(!selectedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return ;
            }
            $.messager.alert("温馨提醒","你确认要入库吗","info",function () {
                $.post("/stockincome/audit",{id:selectedRow.id},function (data) {
                    if(data.success){
                        $.messager.alert("温馨提醒","入库成功","info",function () {
                            cmdObj["reload"]();
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.msg,"error")
                    }
                })
            })
        },
        
        //删除
        stockincomeDelete:function () {
            //1.获取当前行
            var selected = stockincome_datagrid.datagrid("getSelected");
            //2.判断是否选中了当前行
            if(!selected){
                $.messager.alert("温馨提醒","请求选中行","error");
                return;
            }
            console.log(selected);
            //3.删除
            $.messager.alert("温馨提醒","你确定要删除吗","info",function () {

                $.post("stockincome/delete",{id:selected.id},function (data) {
                    console.log(data);
                    if(data.success){
                        $.messager.alert("温馨提醒","删除成功","info",function () {
                            cmdObj["reload"]();
                        })
                    }else {
                        $.messager.alert("温馨提醒",data.message,"error");
                    }
                })
            })


        },
        //筛选
        stockincomeSearch:function () {
            
        },
        //查看详情
        stockincomeView:function () {
            //1.选中行
            var selectedRow = stockincome_datagrid.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return;
            }
            $.post("stockincome/getStockById",{id:selectedRow.id},function (data) {
                console.log(data);
                stockincome_dialog.dialog("open");

                stockincome_dialog_form.form("load",data);
                stockincome_dialog_datagrid.datagrid("loadData",data)
            })

        },
        //选择商品
        addProduct:function () {
            SelectProduct_dialog.dialog("open")
            SelectProduct_dialog.dialog("setTitle","选择商品")
        },
        //保存
        save:function () {

            stockincome_dialog_form.form("submit",{
                url:"/stockincome/save",
                onSubmit:function (param) {
                    var data = stockincome_dialog_datagrid.datagrid("getRows");
                    console.log(data);
                    //遍历所有数据，拼接上传参数
                    for(var i = 0;i<data.length;i++){
                        console.log(data);
                        param['bills['+i+'].id'] = data[i].id;
                        param['bills['+i+'].product.id'] = data[i].productId;
                        param['bills['+i+'].sn'] = data[i].sn;
                        param['bills['+i+'].productName'] = data[i].productName;
                        param['bills['+i+'].unit'] = data[i].unit;
                        param['bills['+i+'].number'] = data[i].number;
                        param['bills['+i+'].costPrice'] = data[i].cosePrice;
                        param['bills['+i+'].amountPrice'] = data[i].amountPrice;

                    }
                },
                success:function (data) {
                    data = $.parseJSON(data);
                    console.log(data)
                    if(data.success){
                        $.messager.alert("温馨提醒","操作成功","info",function () {
                            stockincome_datagrid.datagrid("reload");
                            stockincome_dialog.dialog("close");
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.message,"error");
                    }
                }
            })
        },
        cancel:function () {

        }
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        console.log(cmd);
        cmdObj[cmd]();
    })

})
    //供应商
    function formatterSupplier(value,row,index) {
        return value ? value.name :"";
    }
    //商品名称
    function formatterBrand(value,row,index) {
        return value ? value.name :"";
    }
    //操作人员
    function formatterOperator(value,row,index) {
        return value ? value.name :"";
    }

    //入库状态
    function formatterState(value,row,index) {
        return value == 0?"<span style='color: green'>未入库</span>":"<span style='color: green'>已入库</span>"
    }

    function formatterOperatorStatus(value,row,index) {
        var Action = "<a href='javascript:void(0);'  id='edit' data-options='iconcls:icon-add' onclick='Edit(" + row.ID + ")'>修改</a>\
                            | <a href='javascript:void(0);' onclick='Delete(" + row.ID + ")'>删除</a>";
        return Action;
    }

    function Edit() {
        console.log(1)
    }


function formatterNumber(value) {
    if (value) {
        return dj
            .fs(
                '<input name="messageelementid" id="Checkbox1"  onclick="checkboxbyname(\'messageelementid\',true);" type="checkbox" title={0} value={1} />',
                value, value);
    } else {
        return dj.fs('', value, value);
    }

}

var editIndex = undefined;
function endEditing(){
    if (editIndex == undefined){return true}
    if ($("#stockincome_datagrid").datagrid('validateRow', editIndex)){
        $("#stockincome_datagrid").datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        return false;
    }
}

function onClickCell(index, field){
    if (endEditing()){
        $("#stockincome_datagrid").datagrid('selectRow', index)
            .datagrid('editCell', {index:index,field:field});
        editIndex = index;
    }
}